package com.hnkywl.sts.dao.zjgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.zjgl.Bzj;
import com.hnkywl.sts.entity.zjgl.Hthk;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-4-21
 * Time: 上午10:01
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class HthkDao extends SimpleHibernateTemplate<Hthk, Long> {
    public Page<Hthk> findBzjList(Long gsId, String khMc, String hth, int pageNo, int pageSize) {
        String hql;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Hthk> page = new Page<Hthk>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(gsId);
        }
        if (khMc != null && !("").equals(khMc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" kh.qc like ?");
            param.add("%" + khMc.trim() + "%");
        }
        if (hth != null && !("").equals(hth)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" cght.htbm like ?");
            param.add("%" + hth.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Hthk where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Hthk where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by zt, dateCreated desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    /**
     * 查询绑定的货款
     *
     * @param cghtId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page findNoBindSkList(Long cghtId, int pageNo, int pageSize) {
        Page<Hthk> page = new Page<Hthk>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("sk", "sk", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kxsy", "kxsy", CriteriaSpecification.LEFT_JOIN)
                .createAlias("fkfs", "fkfs", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                                .add(Property.forName("id").as("id"))
                                .add(Property.forName("sk.bm").as("bm"))
			                    .add(Property.forName("sk.fplx").as("fplx"))
                                .add(Property.forName("je").as("sfje"))
                                .add(Property.forName("ye").as("ye"))
                                .add(Property.forName("tx").as("tx"))
                                .add(Property.forName("sk.sfkrq").as("sfkrq"))
                                .add(Property.forName("khfksj").as("khfksj"))
                                .add(Property.forName("txye").as("txye"))
                                .add(Property.forName("fkfs.z").as("fkfs.z"))
                                .add(Property.forName("kxsy.z").as("kxsy"))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        criteria.add(Restrictions.eq("zt", "0"));
        criteria.add(Restrictions.eq("cght.id", cghtId));
        return this.findByCriteria(page, criteria);
    }

    public List<Map> findSumHthkTx(Cght zddhcght) {
        String sql = "select nvl(sum(txye),0) tx,0 as dlf,0 as zjzyf from t_biz_hthk  where txye > 0 and ht_id = ?";
        return (List<Map>) this.createSQLQuery(sql, zddhcght.getId()).setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();
    }

    public List<Hthk> findAllByCght(Cght zddhcght, Jsd jsd) {
        if (null == jsd) {
            String hql = "from Hthk where cght  = ? and jsd.id is null ";
            return this.find(hql, zddhcght);
        } else {
            String hql = "from Hthk where cght  = ? and jsd = ?";
            return this.find(hql, zddhcght, jsd);
        }
    }

    public Hthk findAllHthk(Cght cght, Long sfk_id) {
        String hql = "from Hthk where cght  = ? and zt = '0' and sk_id = ? ";
        List<Hthk> list = this.find(hql, cght, sfk_id);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public Hthk findHthkByCghtId(Long id) {
        String hql = " from Hthk where ht_id = ? ";
        return this.find(hql, id).size() <= 0 ? null : (Hthk) this.find(hql, id).get(0);
    }
    public List<Hthk> findHthkByCght1(Long cghtId) {
        return this.find("from Hthk where  cght.id = ? and zt = '0'  and deleted!=1 order by dateCreated", cghtId);
    }

}
